#include "messagebox.h"
#include <QHBoxLayout>
#include <QLabel>
#include <QPropertyAnimation>
#include <QTimer>
#include <QGraphicsDropShadowEffect>
#include <QDebug>
#include <QDateTime>
#include "logger.h"

namespace WordMemory
{
    MessageBox::MessageBox(QWidget *parent, Log::Type type, const QString &text)
        : QWidget(parent)
        , m_type(type)
        , m_text(text)
    {
        setAttribute(Qt::WA_DeleteOnClose);
        setWindowFlags(Qt::FramelessWindowHint | Qt::Tool);
        setAttribute(Qt::WA_TranslucentBackground);

        setupUi();
        startAnimation();
    }

    void MessageBox::show(QWidget *parent, Log::Type type, const QString &text)
    {
        Log(type, text);
        MessageBox *box = new MessageBox(parent, type, text);
        box->QWidget::show();
    }

    void MessageBox::info(QWidget *parent, const QString &text)  { show(parent, Log::Type::Info, text); }
    void MessageBox::warning(QWidget *parent, const QString &text)  { show(parent, Log::Type::Warning, text); }
    void MessageBox::error(QWidget *parent, const QString &text) { show(parent, Log::Type::Error, text); }

    void MessageBox::setupUi()
    {
        QString backgroundColor;
        QString borderColor;
        QString iconPath;
        QString textColor;

        switch (m_type) {
        case Log::Type::Info:
            backgroundColor = "#e6f7ff";  // 浅蓝色背景
            borderColor = "#91d5ff";      // 蓝色边框
            textColor = "#0050b3";        // 深蓝色文字
            iconPath = ":/icons/info.png";
            break;
        case Log::Type::Warning:
            backgroundColor = "#fffbe6";  // 浅黄色背景
            borderColor = "#ffe58f";      // 橙色边框
            textColor = "#d48806";        // 深橙色文字
            iconPath = ":/icons/warning.png";
            break;
        case Log::Type::Error:
            backgroundColor = "#fff2f0";  // 浅红色背景
            borderColor = "#ffccc7";      // 红色边框
            textColor = "#c41d1d";        // 深红色文字
            iconPath = ":/icons/error.png";
            break;
        }

        // 主容器
        QWidget *container = new QWidget(this);
        container->setStyleSheet(QString(
            "QWidget {"
            "  background-color: %1;"    // 动态背景色
            "  border-radius: 10px;"
            "  padding: 10px;"
            "  border: 1px solid %2;"    // 动态边框色
            "};").arg(backgroundColor, borderColor));

        // 阴影效果
        QGraphicsDropShadowEffect *shadow = new QGraphicsDropShadowEffect(this);
        shadow->setBlurRadius(12);
        shadow->setXOffset(0);
        shadow->setYOffset(4);
        shadow->setColor(QColor(0, 0, 0, 0.1 * 255));
        container->setGraphicsEffect(shadow);

        // 布局
        QHBoxLayout *layout = new QHBoxLayout(container);
        layout->setSpacing(8);
        layout->setContentsMargins(10, 8, 10, 8);

        // 图标
        QLabel *iconLabel = new QLabel(container);
        iconLabel->setPixmap(QPixmap(iconPath).scaled(16, 16, Qt::KeepAspectRatio, Qt::SmoothTransformation));
        layout->addWidget(iconLabel);

        // 文本
        QLabel *textLabel = new QLabel(m_text, container);
        textLabel->setStyleSheet(QString(
            "QLabel {"
            "  font-family: \"Microsoft YaHei\";"
            "  font-size: 15px;"
            "  color: %1;"
            "}").arg(textColor));

        textLabel->setWordWrap(false);
        layout->addWidget(textLabel);
        layout->addStretch();

        // 主布局
        QVBoxLayout *mainLayout = new QVBoxLayout(this);
        mainLayout->addWidget(container);
        mainLayout->setContentsMargins(0, 0, 0, 0);

        // 调整大小
        adjustSize();

        if (parentWidget()) {
            int x = (parentWidget()->width() - width()) / 2;
            int y = parentWidget()->height();
            move(x + 1000, y + 800);
        }

    }

    void MessageBox::startAnimation()
    {
        // 目标位置
        int targetY = parentWidget()->height() + 600;

        // 弹出动画
        QPropertyAnimation *slideUp = new QPropertyAnimation(this, "pos");
        slideUp->setDuration(300);
        slideUp->setStartValue(pos());
        slideUp->setEndValue(QPoint(x(), targetY));
        slideUp->setEasingCurve(QEasingCurve::OutQuad);

        // 消失动画
        QPropertyAnimation *fadeOut = new QPropertyAnimation(this, "windowOpacity");
        fadeOut->setDuration(300);
        fadeOut->setStartValue(1.0);
        fadeOut->setEndValue(0.0);
        fadeOut->setEasingCurve(QEasingCurve::InQuad);

        // 动画序列
        slideUp->start();
        QTimer::singleShot(5300, this, [fadeOut, this]() {
            fadeOut->start();
            connect(fadeOut, &QPropertyAnimation::finished, this, &QWidget::close);
        });
    }
}
